#pragma once
#include "pthread.h"
#include <iostream>
#include <ctime>

typedef void (*callback_t)();
static int num = 1;



class Thread
{
private:
    static void *Routine(void *args)
    {
        Thread *th = static_cast<Thread *>(args);
        th->Entry();

        return nullptr;
    }

public:
    Thread(callback_t cb)
        : _tid(0), _thread_name(""), _start_timestamp(0), _isrunning(false), _cb(cb)
    {
    }

    void Run()
    {
        _thread_name = "Thread-" + std::to_string(num++);
        _start_timestamp = time(nullptr);
        _isrunning = true;
        pthread_create(&_tid, nullptr, Routine, this);
    }

    void Join()
    {
        _isrunning = false;
        pthread_join(_tid, nullptr);
    }

    std::string GetThreadName()
    {
        return _thread_name;
    }

    uint64_t GetStartTimeStamp()
    {
        return _start_timestamp;
    }

    bool IsRunning()
    {
        return _isrunning;
    }
    void Entry()
    {
        _cb();
    }

private:
    pthread_t _tid;
    std::string _thread_name;
    uint64_t _start_timestamp;
    bool _isrunning;
    callback_t _cb;
};